﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Utilities;

namespace HiTeachers.Data
{
    public class StudentFeedbackDao : AbstractNHibernateDao<StudentFeedback, Guid>, IStudentFeedbackDao
    {
        public bool IsAlreadyFeedback(Guid teacherId, string studentEmail)
        {
            try
            {
                const string hsql = "from StudentFeedback stdfb where stdfb.Teacher.ID = :teacherId and stdfb.Email = :studentEmail";

                var query = NHibernateSession.CreateQuery(hsql);

                query.SetGuid("teacherId", teacherId);
                query.SetString("studentEmail", studentEmail);

                return (query.UniqueResult<StudentFeedback>() != null);
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return false;
            }
        }

        public int GetAvgRatingOnTeacher(Guid teacherId)
        {
            try
            {
                const string hsql = "select avg(stdfb.Rate) from StudentFeedback stdfb where stdfb.Teacher.ID = :teacherId and stdfb.IsReviewed = 1";

                var query = NHibernateSession.CreateQuery(hsql);

                query.SetGuid("teacherId", teacherId);

                return Convert.ToInt32(query.UniqueResult());
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return 0;
            }
        }

        public IList<StudentFeedback> GetFeedbacksByTeacher(Guid teacherId)
        {
            try
            {
                const string hsql = "from StudentFeedback stdfb where stdfb.Teacher.ID = :teacherId and stdfb.IsReviewed = 1 order by stdfb.InsertedDate desc";

                var query = NHibernateSession.CreateQuery(hsql);

                query.SetGuid("teacherId", teacherId);

                return query.List<StudentFeedback>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<StudentFeedback>();
            }
        }

        #region For Grid View
        public int TotalNumberOfFeedback(int reviewStatus)
        {
            try
            {
                var hsql = "select count(*) from StudentFeedback stdfb";

                if (reviewStatus != EntityType.AllRequests) hsql += " where stdfb.IsReviewed = :reviewStatus";

                var query = NHibernateSession.CreateQuery(hsql);
                if (reviewStatus != EntityType.AllRequests) query.SetBoolean("reviewStatus", (reviewStatus == EntityType.Reviewed));

                return Convert.ToInt32(query.UniqueResult());
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return 0;
            }
        }

        public IList<StudentFeedback> GetFeedbacks(int reviewStatus)
        {
            try
            {
                var hsql = "from StudentFeedback stdfb";
                if (reviewStatus != EntityType.AllRequests) hsql += " where stdfb.IsReviewed = :reviewStatus";
                hsql += " order by stdfb.InsertedDate desc";

                var query = NHibernateSession.CreateQuery(hsql);
                if (reviewStatus != EntityType.AllRequests) query.SetBoolean("reviewStatus", (reviewStatus == EntityType.Reviewed));


                return query.List<StudentFeedback>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<StudentFeedback>();
            }
        }

        public IList<StudentFeedback> GetFeedbacks(int reviewStatus, int maximumRows, int startRowIndex)
        {
            try
            {
                var hsql = "from StudentFeedback stdfb";
                if (reviewStatus != EntityType.AllRequests) hsql += " where stdfb.IsReviewed = :reviewStatus";
                hsql += " order by stdfb.InsertedDate desc";

                var query = NHibernateSession.CreateQuery(hsql);
                if (reviewStatus != EntityType.AllRequests) query.SetBoolean("reviewStatus", (reviewStatus == EntityType.Reviewed));

                query.SetMaxResults(maximumRows);
                query.SetFirstResult(startRowIndex);

                return query.List<StudentFeedback>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<StudentFeedback>();
            }
        }
        #endregion
    }
}
